<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>滴答办公系统-部门更新</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="media/layui/css/layui.css" media="all">
    <script src="media/js/jquery.min.js"></script>
  </head>
  <body>

    <div class="layui-container" style="margin-top: 5px">
      <form class="layui-form" action="courseadd.do" method="post">
        <div class="layui-form-item">
          <label class="layui-form-label">部门编号</label>
          <div class="layui-input-block">
            <input type="text" name="name" id="did" readonly lay-verify="name" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">部门名称</label>
          <div class="layui-input-block">
            <input type="text" name="week" id="name" lay-verify="name" autocomplete="off" placeholder="请输入部门周期" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">创立日期</label>
          <div class="layui-input-block">
            <input type="text" name="createdate" placeholder="请输入创立日期" id="date" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <input class="layui-btn" style="margin-left: 10%" onclick="modDepart()" value="确认更新">
        </div>
      </form>
    </div>


    <script src="media/layui/layui.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
      function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split("=");
          if (pair[0] == variable) {
            return pair[1];
          }
        }
        return (false);
      }
      
      function init() {
        $.ajax({
          url: "/emp/depart/getDepart",
          type: "POST",
          dataType: "json",
          data: {
            id: getQueryVariable("id")
          },
          success: function(data) {
            var id = data.depart.id;
            var name = data.depart.name;
            var createtime = data.depart.createtime;
            
            $("#did").val(id);
            $("#name").val(name);
            $("#date").val(createtime);
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
          }
        });
      }
      
      init();
      
      function modDepart() {
        var id = $('#did').val();
        var name = $("#name").val();
        var createtime = $("#date").val();
        
        if(name != "" && createtime != "") {
          $.ajax({
            url: "/emp/depart/modDepart",
            type: "POST",
            dataType: "json",
            data: {
              id: id,
              name: name,
              createtime: createtime
            },
            success: function(data) {
              alert("更新部门成功！");
              window.location.reload();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
              alert(XMLHttpRequest.status);
              alert(XMLHttpRequest.readyState);
              alert(textStatus);
            }
          });
        }else{
          alert("请输入部门信息！")
        }
      }
      
      layui.use(
        ['form', 'laydate'],
        function() {
          var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate;

          //日期
          laydate.render({
            elem: '#date'
          });


          //自定义验证规则
          form.verify({
            title: function(value) {
              if (value.length < 5) {
                return '标题至少得5个字符啊';
              }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            content: function(value) {
              layedit.sync(editIndex);
            }
          });
        });
    </script>
  </body>
</html>
